<template>
  <div>
    <el-divider>客户信息列表</el-divider>
    <div class="btn">
      <el-button @click="clearFilter">清除所有过滤器</el-button>
      <el-button
        type="primary"
        @click="
          addCustomer;
          dialogFormVisible = true;
        "
        >新增客户信息</el-button
      >
    </div>
    <el-table ref="filterTable" :data="tableData" style="width: 100%" border>
      <el-table-column prop="cid" label="客户编号" width="100" align="center">
      </el-table-column>
      <el-table-column align="center" prop="cname" label="客户姓名" width="180">
      </el-table-column>
      <el-table-column align="center" prop="cdate" label="创建时间" width="180">
      </el-table-column>
      <el-table-column
        sortable
        prop="brand"
        align="center"
        label="品牌"
        width="140"
        :filters="[
          { text: '比亚迪', value: '比亚迪' },
          { text: '奔腾', value: '奔腾' },
          { text: '奥拓', value: '奥拓' },
        ]"
        :filter-method="filterBrand"
        filter-placement="bottom-end"
      >
        <template slot-scope="scope">
          <el-tag disable-transitions>{{ scope.row.brand }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="industry"
        align="center"
        label="所属行业"
        width="140"
        sortable
        :filters="[
          { text: '金融', value: '金融' },
          { text: '餐饮', value: '餐饮' },
          { text: '教育', value: '教育' },
          { text: 'IT', value: 'IT' },
          { text: '建筑', value: '建筑' },
        ]"
        :filter-method="filterIndustry"
        filter-placement="bottom-end"
      >
        <template slot-scope="scope">
          <el-tag disable-transitions>{{ scope.row.industry }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="saleperson"
        align="center"
        label="销售人员"
        width="160"
        sortable
        :filters="[
          { text: '卡比兽', value: '卡比兽' },
          { text: '蟹老板', value: '蟹老板' },
          { text: '派大星', value: '派大星' },
        ]"
        :filter-method="filterSalePerson"
        filter-placement="bottom-end"
      >
        <template slot-scope="scope">
          <el-tag disable-transitions>{{ scope.row.saleperson }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="sex"
        align="center"
        label="客户性别"
        width="140"
        sortable
        :filters="[
          { text: '男', value: '男' },
          { text: '女', value: '女' },
        ]"
        :filter-method="filterSex"
        filter-placement="bottom-end"
      >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.sex === '男' ? 'primary' : 'danger'"
            disable-transitions
            >{{ scope.row.sex }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="source"
        label="客户来源"
        sortable
        width="180"
        :filters="[
          { text: '线上', value: '线上' },
          { text: '线下', value: '线下' },
        ]"
        :filter-method="filterTag"
        filter-placement="bottom-end"
      >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.source === '线下' ? 'primary' : 'success'"
            disable-transitions
            >{{ scope.row.source }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        align="center"
        label="付款状态"
        sortable
        width="180"
        :filters="[
          { text: '已付款', value: '已付款' },
          { text: '未付款', value: '未付款' },
        ]"
        :filter-method="filterStatus"
        filter-placement="bottom-end"
      >
        <template slot-scope="scope">
          <el-tag
            :type="scope.row.status === '已付款' ? 'primary' : 'danger'"
            disable-transitions
            >{{ scope.row.status }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="
              dialog = true;
              customerUpdate(scope.row.cid);
            "
            type="info"
            icon="el-icon-edit"
            plain
            >修改</el-button
          >
          <el-button
            @click="customerDel(scope.row.cid)"
            type="danger"
            icon="el-icon-delete"
            plain
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-drawer
      title="修改客户信息"
      :before-close="handleClose"
      :visible.sync="dialog"
      direction="rtl"
      custom-class="demo-drawer"
      ref="drawer"
    >
      <div class="demo-drawer__content">
        <el-form :model="form" label-width="120">
          <el-form-item prop="cid" label="客户编号">
            <el-input
              style="width: 215px"
              v-model="form.cid"
              disabled
            ></el-input>
          </el-form-item>
          <el-form-item prop="cname" label="客户姓名">
            <el-input style="width: 215px" v-model="form.cname"></el-input>
          </el-form-item>
          <el-form-item prop="brand" label="汽车品牌">
            <el-select
              @change="brandList"
              v-model="form.brand"
              placeholder="请选择"
            >
              <el-option
                v-for="item in brands"
                :key="item.bid"
                :label="item.brandname"
                :value="item.brandname"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="industry" label="所属行业">
            <el-select
              @change="queryIndustry"
              v-model="form.industry"
              placeholder="请选择"
            >
              <el-option
                v-for="item in industrys"
                :key="item.iid"
                :label="item.iname"
                :value="item.iname"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="saleperson" label="销售人员">
            <el-select
              @change="staffList"
              v-model="form.saleperson"
              placeholder="请选择"
            >
              <el-option
                v-for="item in staff"
                :key="item.sid"
                :label="item.sname"
                :value="item.sname"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="sex" label="客户性别">
            <el-select
              @change="querySex"
              v-model="form.sex"
              placeholder="请选择"
            >
              <el-option
                v-for="item in genderList"
                :key="item.gid"
                :label="item.sex"
                :value="item.sex"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="status" label="是否付款">
            <el-select
              @change="statuList"
              v-model="form.status"
              placeholder="请选择"
            >
              <el-option
                v-for="item in statu"
                :key="item.sid"
                :label="item.status"
                :value="item.status"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="source" label="客户来源">
            <el-select
              @change="querySource"
              v-model="form.source"
              placeholder="请选择"
            >
              <el-option
                v-for="item in sourcelist"
                :key="item.sid"
                :label="item.sourcename"
                :value="item.sourcename"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="cdate" label="创建时间">
            <el-date-picker
              value-format="yyyy-MM-dd"
              v-model="form.cdate"
              type="date"
            ></el-date-picker>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="cancelForm">取 消</el-button>
          <el-button
            type="primary"
            @click="$refs.drawer.closeDrawer()"
            :loading="loading"
            >{{ loading ? "提交中 ..." : "确 定" }}</el-button
          >
        </div>
      </div>
    </el-drawer>
    <el-dialog center title="新增客户信息" :visible.sync="dialogFormVisible">
      <el-form
        label-width="140"
        :model="formData"
        ref="formData"
        :rules="rules"
      >
        <el-form-item label="客户姓名" prop="cname">
          <el-input style="width: 700px" v-model="formData.cname"></el-input>
        </el-form-item>
        <el-form-item label="所属行业" prop="industry">
          <el-select
            style="width: 700px"
            @change="queryIndustry"
            v-model="formData.industry"
            placeholder="请选择客户所属行业"
          >
            <el-option
              v-for="item in industrys"
              :key="item.iid"
              :label="item.iname"
              :value="item.iname"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="saleperson" label="销售人员">
          <el-select
            style="width: 700px"
            @change="staffList"
            v-model="formData.saleperson"
            placeholder="请选择销售员工姓名"
          >
            <el-option
              v-for="item in staff"
              :key="item.iid"
              :label="item.sname"
              :value="item.sname"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="sex" label="客户性别">
          <el-select
            style="width: 700px"
            @change="querySex"
            v-model="formData.sex"
            placeholder="请选择"
          >
            <el-option
              v-for="item in genderList"
              :key="item.gid"
              :label="item.sex"
              :value="item.sex"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="status" label="是否付款">
          <el-select
            style="width: 700px"
            @change="statuList"
            v-model="formData.status"
            placeholder="请选择"
          >
            <el-option
              v-for="item in statu"
              :key="item.sid"
              :label="item.status"
              :value="item.status"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="source" label="客户来源">
          <el-select
            style="width: 700px"
            @change="querySource"
            v-model="formData.source"
            placeholder="请选择"
          >
            <el-option
              v-for="item in sourcelist"
              :key="item.sid"
              :label="item.sourcename"
              :value="item.sourcename"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item prop="brand" label="汽车品牌">
          <el-select
            style="width: 700px"
            @change="brandList"
            v-model="formData.brand"
            placeholder="请选择"
          >
            <el-option
              v-for="item in brands"
              :key="item.bid"
              :label="item.brandname"
              :value="item.brandname"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCustomer">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import httpApi from "@/http";
import moment from "moment";
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      dialog: false,
      loading: false,
      timer: null,
      formData: {
        brand: "",
        cdate: "",
        cname: "",
        industry: "",
        saleperson: "",
        sex: "",
        source: "",
        status: "",
      },
      rules: {
        brand: [{ required: true, message: "必填", trigger: "blur" }],
        cname: [{ required: true, message: "必填", trigger: "blur" }],
        industry: [{ required: true, message: "必填", trigger: "blur" }],
        saleperson: [{ required: true, message: "必填", trigger: "blur" }],
        sex: [{ required: true, message: "必填", trigger: "blur" }],
        source: [{ required: true, message: "必填", trigger: "blur" }],
        status: [{ required: true, message: "必填", trigger: "blur" }],
      },
      form: {
        brand: "",
        cdate: "",
        cid: 1,
        cname: "",
        industry: "",
        saleperson: "",
        sex: "",
        source: "",
        status: "",
      },
      brands: [],
      industrys: [],
      staff: [],
      statu: [],
      sourcelist: [],
      genderList: [],
      person: [],
    };
  },
  methods: {
    clearFilter() {
      this.$refs.filterTable.clearFilter();
    },
    filterTag(value, row) {
      return row.source === value;
    },
    filterSalePerson(value, row) {
      return row.saleperson === value;
    },
    filterStatus(value, row) {
      return row.status === value;
    },
    filterSex(value, row) {
      return row.sex === value;
    },
    filterBrand(value, row) {
      return row.brand === value;
    },
    filterIndustry(value, row) {
      return row.industry === value;
    },
    customerQuery() {
      let params = {
        start: 1,
        pageCount: 5,
      };
      httpApi.customerApi.customerList(params).then((res) => {
        // console.log(res);
        this.tableData = res.data.data;
      });
    },
    customerDel(cid) {
      this.$confirm("确认删除该客户信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpApi.customerApi.customerDelete({ cid }).then((res) => {
            if (res.data.code == 200) {
              this.$message({ type: "success", message: "客户信息删除成功" });
              this.$router.go(0);
            } else {
              this.$message({ type: "error", message: "客户信息删除失败" });
            }
          });
        })
        .catch(() => {});
    },
    handleClose(done) {
      if (this.loading) {
        return;
      }
      // console.log(done);
      this.$confirm("确认修改吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(this.form);
          httpApi.customerApi.customerUpdate(this.form).then((res) => {
            if (res.data.code == 200) {
              this.$message({ type: "success", message: "客户信息修改成功" });
              this.$router.go(0);
            } else {
              this.$message({ type: "error", message: "客户信息修改失败" });
            }
          });
          this.loading = true;
          this.timer = setTimeout(() => {
            done();
            // 动画关闭需要一定的时间
            setTimeout(() => {
              this.loading = false;
            }, 400);
          }, 1000);
        })
        .catch(() => {});
    },
    cancelForm() {
      this.loading = false;
      this.dialog = false;
      clearTimeout(this.timer);
    },
    customerUpdate(cid) {
      httpApi.customerApi.customerQueryById({ cid }).then((res) => {
        this.form = res.data.data[0];
      });
    },
    brandList() {
      httpApi.estimateApi.queryCarBrand().then((res) => {
        this.brands = res.data.data;
      });
    },
    queryIndustry() {
      httpApi.customerApi.industryList().then((res) => {
        // console.log("行业列表", res);
        this.industrys = res.data.data;
      });
    },
    staffList() {
      httpApi.customerApi.staffList().then((res) => {
        this.staff = res.data.data;
      });
    },
    statuList() {
      httpApi.customerApi.statu().then((res) => {
        // console.log(res);
        this.statu = res.data.data;
      });
    },
    querySource() {
      httpApi.customerApi.querySource().then((res) => {
        this.sourcelist = res.data.data;
      });
    },
    querySex() {
      httpApi.customerApi.querySex().then((res) => {
        this.genderList = res.data.data;
      });
    },
    addCustomer() {
      let cdate = new Date();
      this.formData.cdate = moment(cdate).format("yyyy-MM-DD");
      console.log(this.formData);
      this.$refs.formData.validate((valid) => {
        if (valid) {
          httpApi.customerApi.customerAdd(this.formData).then((res) => {
            if (res.data.code == 200) {
              this.$message({ type: "success", message: "新增客户信息成功" });
              this.dialogFormVisible = false;
              this.$router.go(0);
            } else {
              this.$message({ type: "error", message: "新增客户信息失败" });
            }
          });
        }
      });
    },
  },
  mounted() {
    this.customerQuery();
    this.brandList();
    this.queryIndustry();
    this.staffList();
    this.statuList();
    this.querySource();
    this.querySex();
  },
};
</script>

<style lang="scss" scoped>
.btn {
  margin-bottom: 10px;
}
::v-deep .el-drawer__header {
  text-align: center;
}

::v-deep .demo-drawer__footer {
  text-align: center;
}
.el-form {
  margin: 0 20px;
}
.el-form-item__content {
  text-align: center;
}
::v-deep .el-drawer {
  width: 18% !important;
}
</style>